<?php
/**
 * Created by JetBrains PhpStorm.
 * User: patch
 * Date: 07.12.12
 * Time: 2:16
 * To change this template use File | Settings | File Templates.
 */

module_load_include('inc', 'node', 'node.pages');

/**
 * @return bool|string
 */
function private_ads_sections() {
  $vocabulary = taxonomy_vocabulary_machine_name_load(PRIVATE_AD_SECTIONS_VOCABULARY);
  $parents = taxonomy_get_tree($vocabulary->vid, 0, 1);
  $columns = array();


  $i = 0;
  $index = 0;

  foreach($parents as $parent) {

    if($i == ceil(count($parents) / PRIVATE_AD_SECTION_COLUMNS_COUNT)) {
      $index++;
      $i = 0;
    }

    $columns[$index][$i] = array(
      'title' => $parent->name,
      'items' => array(),
    );

    $childens = taxonomy_get_children($parent->tid);

    foreach($childens as $childen) {
      $columns[$index][$i]['items'][] = taxonomy_term_load($childen->tid);
    }

    $i++;
  }

  $out = theme('private_ad_sections', array('columns' => $columns));

  return render($out);
}

/**
 * @param bool $section
 * @param bool $category
 * @return bool|string
 */
function private_ads_overview_page_callback($section = FALSE, $category = FALSE) {

  $nids = array();
  $nodes = private_ad_load_multiple($nids, array('status' => 1));

  $out = '';
  if($nodes) {
    $views = node_view_multiple($nodes, 'teaser');
    $out = '<div class="row">' . render($views) . '</div>';
    $out .= theme('pager');
  }

  return render($out);
}

/**
 * @return array|string
 */
function private_ads_owner_overview_page() {

  $user = aia_user_load();
  $out = array();

  $headers = array(
    'title' => array('data' => t('Title'), 'field' => 'title'),
    'created' => array('data' => t('Created'), 'field' => 'created'),
    'description' => array('data' => t('Description')),
    'operations' => t('Действия'),
  );

  if($user) {
    $nids = db_select('node', 'n')
      ->fields('n')
      ->condition('uid', $user->uid)
      ->condition('type', PRIVATE_AD_NODE_TYPE)
      ->extend('PagerDefault')
      ->extend('tableSort')
      ->limit(10) // todo: вынести в настройки
      ->orderBy('created')
      ->orderByHeader($headers)
      ->execute()->fetchCol();

    $nodes = node_load_multiple($nids);

    $rows = array();
    $destination = drupal_get_destination();

    foreach ($nodes as $node) {

      $operations['edit'] = array(
        'title' => t('edit'),
        'href' => 'account/' . PRIVATE_AD_BASE_PATH_ALIAS . '/' . $node->nid . '/edit',
        'query' => $destination,
      );

      $operations['delete'] = array(
        'title' => t('delete'),
        'href' => 'account/' . PRIVATE_AD_BASE_PATH_ALIAS . '/' . $node->nid . '/delete',
        'query' => $destination,
      );

      $rows[] = array(
        'title' => l($node->title, 'node/' . $node->nid),
        'created' => format_date($node->created),
        'description' => 'description',
        'operations' => array(
          'data' => array(
            '#theme' => 'links__node_operations',
            '#links' => $operations,
            '#attributes' => array('class' => array('links', 'inline')),
          ),
        ),
      );
    }

    $out['table'] = array(
      '#theme' => 'table',
      '#header' => $headers,
      '#rows' => $rows,
      '#attributes' => array(
        'class' => array('ads-owner-list'),
      ),
      '#empty' => t('Объявлений не найдено!'),
    );

    $out['pager'] = array('#theme' => 'pager');

  }

  return $out;
}